#!/usr/bin/env python
# -*- coding: utf-8 -*-

# created by: lavenliu
# created date: 2016-01-22


import os
import sys

MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24


def which(program):
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
    
    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None


class bcolors:
    HEADER = "\033[1;35m"
    OKBLUE = "\033[1;34m"
    OKGREEN = "\033[1;32m"
    WARNING = "\033[1;33m"
    FAIL = "\033[1;31m"
    ENDC = "\033[m"
    
    def disable(self):
        self.HEADER = ""
        self.OKBLUE = ""
        self.OKGREEN = ""
        self.WARNING = ""
        self.FAIL = ""
        self.ENDC = ""


def purple_str(str):
    return "%s%s%s" % (bcolors.HEADER, str, bcolors.ENDC)


def green_str(str):
    return "%s%s%s" % (bcolors.OKGREEN, str, bcolors.ENDC)


def blue_str(str):
    return "%s%s%s" % (bcolors.OKBLUE, str, bcolors.ENDC)


def yellow_str(str):
    return "%s%s%s" % (bcolors.WARNING, str, bcolors.ENDC)


def red_str(str):
    return "%s%s%s" % (bcolors.FAIL, str, bcolors.ENDC)


MSG_OK = bcolors.OKGREEN + "SUCCESS:%s" + bcolors.ENDC
MSG_INFO = bcolors.OKGREEN + "INFO:%s" + bcolors.ENDC
MSG_WARN = bcolors.WARNING + "WARNING:%s" + bcolors.ENDC
MSG_FAIL = bcolors.FAIL + "FAIL:%s" + bcolors.ENDC


def print_time(label, time):
    days = float(time) / DAY
    hours = (float(time) % DAY) / HOUR
    minutes = (float(time) % HOUR) / MINUTE
    seconds = (float(time) % MINUTE)

    print("%s: [%d days] + [%d hours %d minutes %d seconds]" % (label,
                                                                int(days),
                                                                int(hours),
                                                                int(minutes),
                                                                int(seconds)))


# get the system kernel version
with open('/proc/sys/kernel/version') as f:
    sys_version = f.readline()
    

# 获取主机名
with open('/proc/sys/kernel/hostname') as f:
    hostname = f.readline()
    

# 获取内核版本号
with open('/proc/sys/kernel/osrelease') as f:
    os_release = f.readline()


# 获取启动启动时间
with open('/proc/uptime') as f:
    uptime_second, idle_second = f.readline().split()


if __name__ == '__main__':
    print(sys_version)
    print(os_release)
    print(hostname)
    print_time("uptime", uptime_second)
